package mips;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	
	public static void main(String[]args) throws Exception
	{
   
       readData();
		readMemory();
   
   
   
		FileInputStream fstream = new FileInputStream("code.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		String Instructions = "";
		while ((strLine = br.readLine()) != null)
			Instructions += (strLine + "\n");
		

	     String [] splitterInstructions = Instructions.split("\n");
	     
	     int pcvalue = Integer.parseInt(splitterInstructions[0].split(" ")[1]);
	     PC.pc = pcvalue;
	     PC.pcInitial = pcvalue;
	   InstructionMemory.Instructions = splitterInstructions;
	   
	    try
	   {
	   
	   int i = 0;
	   while ((PC.pc - PC.pcInitial) / 4 < InstructionMemory.Instructions.length - 1) {
          System.out.println("<<<CLOCK: " + (i) + ">>>");
		  Decoder.decode(InstructionMemory.Instructions[(PC.pc - PC.pcInitial) / 4 + 1]);
		  System.out.println("///////////////////////////////////////////////////////////////");
		  i++;
	   }
	   
	   }
	   catch(Exception e)
	   {
		   System.out.println("error");
	   }
	     
	     
	}
   
   private static void readMemory() throws Exception {
		FileInputStream fstream = new FileInputStream("memory.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		br.readLine(); br.readLine();
		while ((strLine = br.readLine()) != null) {
			String[] split = strLine.split(":");
			Memory.dataMemory.add(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
		}
	}

	private static void readData() throws Exception {
		FileInputStream fstream = new FileInputStream("data.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String strLine;
		int i = 1;
		while ((strLine = br.readLine()) != null) {
			Register.registers[i] = Integer.parseInt(strLine.split("=")[1]);
			i++;
		}
		
	}

}
